var gulp = require('gulp'),
    path = require('path'),
    del = require('del'),
    compass = require('gulp-compass'),
    revmap = require('gulp-revmap'),
    uglify = require('gulp-uglify'),
    minifyCSS = require('gulp-minify-css'),
    rename = require('gulp-rename'),
    browserify = require('gulp-browserify'),
    watch = require('gulp-watch');

var paths = {
    assets: '../source/assets',
    // develope folders
    _script: './_javascript',
    _scss: './_scss',
    // reference folders
    script: './javascript',
    css: './stylesheet',
    image: './_images',
    font: './_font'
};

var files = {
    assetsFiles: paths.assets + '/*',
    // reference files
    imageFiles: paths.image + '/*',
    fontFiles: paths.font + '/**/*',
    cssFiles: paths.css + '/**/*',
    scriptFiles: paths.script + '/**/*',
    // develope files
    _scssFiles: paths._scss + '/*.scss',
    _scriptFiles: paths._script + '/*.js'
};

// delete assets files
gulp.task('cleanAssets', function(cb) {
    del([files.assetsFiles], {force: true}, cb);
});
// copy libs
gulp.task('copy', ['cleanAssets'], function() {
    return gulp.src([
                    files.fontFiles,
                    files.imageFiles,
                    files.cssFiles,
                    files.scriptFiles
                ])
               .pipe(gulp.dest(paths.assets));
});
gulp.task('_scss2css', function() {
    return gulp.src(files._scssFiles)
               .pipe(compass({
                    config_file: './config.rb',
                    environment: 'development',
                    css: paths.assets,
                    sass: paths._scss,
                    logging: true
               }))
               .on('error', function(err) {
                    console.log('')
               })
               .pipe(gulp.dest(paths.assets));
});
// javascript file
gulp.task('_scripts',  function() {
    return gulp.src(files._scriptFiles)
        .pipe(browserify())
        .on('error', function(err) {
            console.log('');
        })
        .pipe(gulp.dest(paths.assets));
});
// generate bust
gulp.task('startup_md5', ['copy', '_scss2css', '_scripts'], function() {
    return gulp.src(files.assetsFiles)
        .pipe(revmap('assets_hashmap.json', {hashlen: 10}))
        .pipe(gulp.dest(paths.assets));
});

gulp.task('css2md5', ['_scss2css'], function() {
    return gulp.src(files.assetsFiles)
        .pipe(revmap('assets_hashmap.json', {hashlen: 10}))
        .pipe(gulp.dest(paths.assets));
});
gulp.task('_script2md5', ['_scripts'], function() {
    return gulp.src(files.assetsFiles)
        .pipe(revmap('assets_hashmap.json', {hashlen: 10}))
        .pipe(gulp.dest(paths.assets));
});
// watch files
gulp.task('_watch', function() {
    watch({glob: paths._scss + '/**/*.scss'}, function() {
        gulp.start('css2md5');
    });
    watch({glob: paths._script + '/**/*.js'}, function() {
        gulp.start('_script2md5');
    });
});

// release
gulp.task('release', ['startup_md5']);
// default task && development
gulp.task('default', ['startup_md5', '_watch']);
